package com.loong.coupon.business;

import com.loong.common.enumerate.CouponStatus;
import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.coupon.common.backend.param.CouponQueryParam;
import com.loong.coupon.model.Coupon;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2021/1/7 15:49
 */
@Repository
public class CouponBusiness extends AbstractBusiness<Coupon> {

    /**
     * 根据参数查询优惠券
     * @param param 优惠券查询参数
     * @return 优惠券集合
     */
    public List<Coupon> findCouponByParam(CouponQueryParam param){
        Condition condition = new Condition(Coupon.class);
        Example.Criteria criteria = condition.createCriteria();
        if (ObjectUtils.isNotEmpty(param.getCouponName())){
            //优惠券名称
            criteria.andLike("couponName","%"+param.getCouponName()+"%");
        }
        if (ObjectUtils.isNotEmpty(param.getCouponMoney())){
            //面额
           criteria.andEqualTo("couponMoney",param.getCouponMoney());
        }
        if (ObjectUtils.isNotEmpty(param.getColor())){
            //颜色
            criteria.andEqualTo("color",param.getColor());
        }
        if (ObjectUtils.isNotEmpty(param.getDiscountCondition())){
            //满减价格
            criteria.andEqualTo("discountCondition",param.getDiscountCondition());
        }
        if (ObjectUtils.isNotEmpty(param.getValidType())){
            //有效类型
            criteria.andEqualTo("validType",param.getValidType());
        }
        if (ObjectUtils.isNotEmpty(param.getDiscountPlatformId())){
            //优惠平台id
            criteria.andEqualTo("discountPlatformId",param.getDiscountPlatformId());
        }
        if (ObjectUtils.isNotEmpty(param.getCouponSource())){
            //优惠券来源
            criteria.andEqualTo("couponSource",param.getCouponSource());
        }
        if (ObjectUtils.isNotEmpty(param.getReturnBackYes())){
            //优惠券是否可退
            criteria.andEqualTo("returnBackYes",param.getReturnBackYes());
        }
        if (ObjectUtils.isNotEmpty(param.getCouponType())){
            //优惠券类型
            criteria.andEqualTo("couponType",param.getCouponType());
        }
        if (ObjectUtils.isNotEmpty(param.getStatus())){
            //优惠券状态
            criteria.andEqualTo("status",param.getStatus());
        }
        if (ObjectUtils.isNotEmpty(param.getStartTime())){
            //创建时间
            criteria.andGreaterThanOrEqualTo("createTime",param.getStartTime());
        }
        if (ObjectUtils.isNotEmpty(param.getEndTime())){
            //创建时间
            criteria.andLessThanOrEqualTo("createTime",param.getEndTime());
        }
        return findByCondition(condition);
    }

    /**
     * 根据优惠券id查询审核已通过的优惠券
     * @param couponId 优惠券id
     * @return 优惠券集合
     */
    public List<Coupon> findApprovedCouponByCouponId(Long couponId){
        Condition condition = new Condition(Coupon.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("id",couponId);
        criteria.andEqualTo("status", CouponStatus.APPROVED.getCode());
        return findByCondition(condition);
    }
}
